package com.zyz.N_反射机制.Class常用方法;

import java.lang.reflect.Field;

/**
 * @author: vick
 * @Date: 2025/4/14
 * @Description:
 * @version: 1.0
 */
public class Demo3 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {

        //获取Class类实例
        Class clazz = Class.forName("com.vick.N_反射机制.Class常用方法.Person");
        //根据默认构造函数获取Class对象的一个实例
        Object object = clazz.newInstance();

        //获取名字为 name 的成员变量
        Field field = clazz.getField("name");
        //给 成员变量赋值
        field.set(object, "aaaa");
        //获取这个成员变量的 值
        Object name = field.get(object);
        System.out.println(name);
    }
}
